# coding=utf-8
# @time     :2021/12/16
# @Author   :Simple
# @FileName :base_api
import requests

from util.handle_yaml import get_api
from util.logger import log


class BaseApi:
    def __init__(self, session=None):
        if session is None:
            # 创建一个session，所有请求使用同一个session，给请求提速
            self.s = requests.Session()
        else:
            self.s = session
        # self.token = self.get_token()
        # self.s.params = {'access_token': self.token}  # 共同参数放入session中，session会自动带入请求

    # def get_token(self):
    #     url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    #     params = {
    #         "corpid": "ww4d6b32902cc70493",
    #         "corpsecret": "XSxBlAgRJAbyiVwR4YxNH1NmT9W8F_1ilYG4VjOJV_I"
    #     }
    #     r = self.s.get(url, params=params)
    #     return r.json()['access_token']

    def send(self, *args, **kwargs):
        return self.s.request(*args, **kwargs)

    def send_request(self, file, data):
        """
        发送请求
        :param file: api yaml文件的路径
        :param data: 请求数据
        :return: json格式的response
        """
        api = get_api(file, data)
        if self.s is None:
            return requests.request(**api).json()
        else:
            return self.s.request(**api).json()

